#include "gameModes.h"

uint8_t playerScores[8];

PLAYER checkDropedStone(void){
	 if ( STONE_DROP_PLAYER1 ){ 
		return PLAYER_1;
	} else if ( STONE_DROP_PLAYER2) {
		return PLAYER_2;
	} else if ( STONE_DROP_PLAYER3) {
		return PLAYER_3;		
	} else if ( STONE_DROP_PLAYER4) {
		return PLAYER_4;		
	} else if ( STONE_DROP_PLAYER5) {
		return PLAYER_5;		
	} else if ( STONE_DROP_PLAYER6) {
		return PLAYER_6;		
	} else if ( STONE_DROP_PLAYER7) {
		//return PLAYER_7;		
	} else if ( STONE_DROP_PLAYER8) {
		//return PLAYER_8;
	}  // end if
	return NO_ACTION;
	
} // end checkDropedStone

void resetGame(void){
	for(int i = 0; i < 7; i++) {
		playerScores[i] = 0;
	}
}


void raisePlayer(PLAYER player){
	if (playerScores[player] < 3) {
		playerScores[player]++;
	}
}


uint8_t playersLeft(void) {
	uint8_t playersLeft = 0;

	for(int i = 0; i < 7; i++) {
		if (playerScores[i] < 3) {
			playersLeft++;
		} else {
			ctrlDiode(i, TURN_ON);
		}
	}

	return playersLeft;
}

void ctrlDiode(PLAYER player, DIODE_ACTION mode) {
	uint8_t  myDiode = 0;
	volatile unsigned char *myPort = 0;

	switch(player) {
		case PLAYER_1:
			myDiode = DIODE_P1;
			myPort  = &PORTC;
			break;
		case PLAYER_2:
			myDiode = DIODE_P2;
			myPort  = &PORTC;
			break;

		case PLAYER_3:
			myDiode = DIODE_P3;
			myPort  = &PORTC;
			break;

		case PLAYER_4:
			myDiode = DIODE_P4;
			myPort  = &PORTC;
			break;

		case PLAYER_5:
			myDiode = DIODE_P5;
			myPort  = &PORTC;
			break;

		case PLAYER_6:
			myDiode = DIODE_P6;
			myPort  = &PORTC;
			break;

		case PLAYER_7:
			myDiode = DIODE_P7;
			myPort  = &PORTD;
			break;

		case PLAYER_8:
			myDiode = DIODE_P8;
			myPort  = &PORTD;
			break;
		
		default:
			break;

	} // end switch


	switch(mode) {
		case FLASH:
			*myPort |= myDiode;
			_delay_ms(20);
			*myPort &= !myDiode;
			_delay_ms(20);
			*myPort |= myDiode;
			_delay_ms(20);
			*myPort &= !myDiode;
			_delay_ms(20);
			*myPort |= myDiode;
			_delay_ms(20);
			*myPort &= !myDiode;
			break;

		case TURN_ON:
			*myPort |= myDiode;
			break;

	    case TURN_OFF:
			*myPort &= !myDiode;
			break;
	} // end switch

} // end ctrlPlayerDiode

















